<?php

namespace App\CompanyApi\Resources;

use App\Http\Resources\PaginatedResourceCollection;
use App\Models\JobpositionModel;
use Illuminate\Support\Collection;

class JobpositionResource extends PaginatedResourceCollection
{


    public function collectionList(Collection $collection)
    {
        $list = [];
        foreach ($collection as $item) {
            $list[] = [
                'id' => $item->id,
                'position_name' => $item->position_name,
                'region' => $item->regionInfo(),
                'salary' => (string)$item->salary,
                'salary_type' => JobpositionModel::SALARY_TYPE_LIST[$item->salary_type],
                'cur_status' => $item->cur_status,
                'pv' => $item->statistics->pv ?? 0,
                'uv' => $item->statistics->uv ?? 0,
                'recruit_num' => $item->recruit_num,
                'selection_date_start' => $item->selection_date_start,
                'selection_date_end' => $item->selection_date_end,
                'work_time_start' => $item->work_time_start,
                'work_time_end' => $item->work_time_end,
                'contact_type' => $item->contact_type,
                'contactPhone' => $this->contactPhone($item->contactPhone),
                'contactQq' => $this->contactQq($item->contactQq),
                'contactWechat' => $this->contactWechat($item->contactWechat),
                'updated_at' => $item->updated_at->format('Y-m-d H:i:s'),
                'created_at' => $item->created_at->format('Y-m-d H:i:s'),
            ];
        }
        return $list;
    }

    private function contactPhone($contactPhone)
    {
        $res = [];
        foreach ($contactPhone as $phone) {
            $res = [
                'phone_number' => $phone->phone_number,
                'number' => $phone->number,
                'recruit_num' => $phone->recruit_num,
            ];
        }
        return $res;
    }

    private function contactQq($contactQq)
    {
        $res = [];
        foreach ($contactQq as $qq) {
            $res = [
                'qq_number' => $qq->qq_number,
                'qq_qr_code' => $qq->qq_qr_code,
                'number' => $qq->number,
                'recruit_num' => $qq->recruit_num,
            ];
        }
        return $res;
    }

    private function contactWechat($contactWechat)
    {
        $res = [];
        foreach ($contactWechat as $wechat) {
            $res = [
                'wechat_number' => $wechat->wechat_number,
                'wechat_qr_code' => $wechat->wechat_qr_code,
                'number' => $wechat->number,
                'recruit_num' => $wechat->recruit_num,
            ];
        }
        return $res;
    }

}
